package bc.running.forum.db;

import static bc.running.forum.db.Constants.*;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Clob;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class TopicsTableHandler implements ITableHandler {

	public TopicsTableHandler() {
		props = new Properties();
	}
	@Override
	public List<String> handleTableData(ResultSet resultSet) throws Exception {
		if(resultSet == null) {
			return null;
		}
		BufferedWriter postsOut = new BufferedWriter(new OutputStreamWriter
				  (new FileOutputStream(new File("out/" + tables[0] + ".xml")),"UTF8"));
		Utils.writeln(postsOut,"<" + tables[0] + ">");
		
		BufferedWriter postTextOut = new BufferedWriter(new OutputStreamWriter
				  (new FileOutputStream(new File("out/" + tables[1] + ".xml")),"UTF8"));
		Utils.writeln(postTextOut,"<" + tables[1] + ">");
		
		BufferedWriter topicsOut = new BufferedWriter(new OutputStreamWriter
				  (new FileOutputStream(new File("out/" + tables[2] + ".xml")),"UTF8"));
		Utils.writeln(topicsOut,"<" + tables[2] + ">");
		int offset = 0;
		while(resultSet.next()) {
			Utils.writeln(postsOut,"<row>");
			Utils.writeln(postTextOut,"<row>");
			Utils.writeln(topicsOut,"<row>");
			
			int topicId = resultSet.getInt(TOPIC_ID);
			int forumId = resultSet.getInt(FORUM_ID);
			String _date = resultSet.getString(T_DATE);
			int date = Utils.convertDate(_date);
			Clob titleClob = resultSet.getClob(T_SUBJECT);
			String title = Utils.getStringFromClob(titleClob);
			Clob messageClob = resultSet.getClob(T_MESSAGE);
			String message = Utils.getStringFromClob(messageClob);
			String _ip = resultSet.getString(T_IP);
			String ip = Utils.encodeIP(_ip);
			int viewCount = resultSet.getInt(T_VIEW_COUNT);
			int replies = resultSet.getInt(T_REPLIES);
			int author = resultSet.getInt(T_AUTHOR);
			
			buildPHPBBTopicsRecord(topicsOut,topicId,forumId,date,title,viewCount,replies,offset,author);
			buildPHPBBPostsRecord(postsOut,offset,topicId,forumId,author,ip,date);
			buildPHPBBPostsTextRecord(postTextOut,offset,title,message,topicId);
			
			Utils.writeln(postsOut,"</row>");
			Utils.writeln(postTextOut,"</row>");
			Utils.writeln(topicsOut,"</row>");
			offset++;
			
		}
		Utils.writeln(postsOut,"</" + tables[0] + ">");
		postsOut.close();
		Utils.writeln(postTextOut,"</" + tables[1] + ">");
		postTextOut.close();
		Utils.writeln(topicsOut,"</" + tables[2] + ">");
		topicsOut.close();
		props.store(new FileOutputStream("tmp.properties"), "");
		return Arrays.asList(tables);
	}

	//http://www.phpbbdoctor.com/doc_columns.php?id=10
	private void buildPHPBBPostsTextRecord(BufferedWriter postTextOut,
			int offset, String title, String message,int topicId) throws Exception {
		String subject = Utils.xmlEscaping(title);
		handleField(postTextOut, "post_subject", subject);
		handleField(postTextOut, "post_text", Utils.xmlEscaping(message));
		handleField(postTextOut, "post_id", offset);
		writeProperty(topicId,subject);
		handleField(postTextOut, "bbcode_uid", "");
	}
	
	private void writeProperty(int topicId, String subject) {
		props.put(new Integer(topicId).toString(), subject);
		
	}

	//http://www.phpbbdoctor.com/doc_columns.php?id=21
	private void buildPHPBBTopicsRecord(BufferedWriter topicsOut, int topicId,
			int forumId, int date, String title, int viewCount, int replies, int offset,int author) throws Exception {
		handleField(topicsOut, "topic_title", Utils.xmlEscaping(title));
		handleField(topicsOut, "topic_id",topicId);
		handleField(topicsOut, "forum_id",forumId);
		handleField(topicsOut, "topic_time",date);
		handleField(topicsOut, "topic_views",viewCount);
		handleField(topicsOut, "topic_replies",replies);
		handleField(topicsOut, "topic_first_post_id",offset);
		handleField(topicsOut, "topic_last_post_id",offset);
		handleField(topicsOut, "topic_poster",author);
		String [] zeroFieldsNames =  new String [] {"topic_moved_id","topic_status","topic_vote","topic_type"};
		for (String fieldName : zeroFieldsNames) {
			handleField(topicsOut, fieldName, 0);
		}

	}
	//http://www.phpbbdoctor.com/doc_columns.php?id=9
	private void buildPHPBBPostsRecord(BufferedWriter postsOut, int replyId,
			int topicId, int forumId, int author, String ip, int date) throws Exception {
		handleField(postsOut, "post_id",replyId);
		handleField(postsOut, "topic_id",topicId);
		handleField(postsOut, "forum_id",forumId);
		handleField(postsOut, "poster_id",author);
		handleField(postsOut, "poster_ip",ip);
		handleField(postsOut, "post_time",date); 
		String [] zeroFieldsNames =  new String [] {"enable_html","enable_sig","post_edit_count"};
		for (String fieldName : zeroFieldsNames) {
			handleField(postsOut,fieldName,0);
		}
		String [] oneFieldsNames =  new String [] {"enable_bbcode","enable_smilies"};
		for (String fieldName : oneFieldsNames) {
			handleField(postsOut,fieldName,1);
		}
		String [] nullFieldsNames =  new String [] {"post_username","post_edit_time"};
		for (String fieldName : nullFieldsNames) {
			handleField(postsOut,fieldName,null);
		}
	}

	
	private void handleField(BufferedWriter writer,String fieldName,Object fieldValue) throws Exception {
		String tmp = String.format(Utils.FIELD_TEMPLATE, fieldName,fieldValue);
		Utils.writeln(writer, tmp);

	}
	private Properties props = null;
	private static final String [] tables = new String []{"phpbb_posts","phpbb_posts_text","phpbb_topics"};

}
